package com.mh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mh.common.JwtComponent;
import com.mh.common.QueryPageParam;
import com.mh.common.Result;
import com.mh.entity.Menu;
import com.mh.entity.User;
import com.mh.service.MenuService;
import com.mh.service.UserService;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2022-11-28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Resource
    private JwtComponent jwtComponent;
    //查询所有
    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }
    //根据no查询用户是否存在
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no){
        List list=userService.lambdaQuery().eq(User::getNo,no).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return userService.save(user)?Result.suc():Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //更新
    @RequestMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }
    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id){
        return userService.removeById(id)?Result.suc():Result.fail();
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        List list=userService.lambdaQuery()
                .eq(User::getNo,user.getNo())
                .eq(User::getPassword,user.getPassword()).list();


        if(list.size()>0){
            User user1= (User) list.get(0);

            //登录成功，生成jwt token
            String token = jwtComponent.sign(user1.getPhone(),user1.getPassword());

            List menuList=menuService.lambdaQuery().like(Menu::getMenuRight,user1.getRoleId()).list();
            HashMap res=new HashMap();
            res.put("user",user1);
            res.put("menu",menuList);
            //返回token给前端
            res.put("token",token);
            return Result.suc(res);
        }
        return Result.fail();
    }
    //新增或修改(有就修改，没有就新增)
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //删除
//    @DeleteMapping("delete")
//    public boolean delete(Integer id){
//        return userService.removeById(id);
//    }
    //查询（模糊，匹配）
    @PostMapping("/listP")
    public Result listP(@RequestBody User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(user.getName())){
            lambdaQueryWrapper.like(User::getName,user.getName());
        }
        return Result.suc(userService.list(lambdaQueryWrapper));
    }
    //分页查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam queryPageParam){
        HashMap param = queryPageParam.getParam();
        String  name = (String) param.get("name");
        String  sex = (String) param.get("sex");
        String  roleId = (String) param.get("roleId");


        Page<User> page=new Page<>();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name)){
            queryWrapper.like(User::getName,name);
        }
        if(StringUtils.isNotBlank(sex)){
            queryWrapper.eq(User::getSex,sex);
        }
        if(StringUtils.isNotBlank(roleId)){
            queryWrapper.eq(User::getRoleId,roleId);
        }
        IPage result = userService.page(page, queryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }


}
